#include "pageswidget.h"

#include <QMouseEvent>
#include <QMimeData>
#include <QPlainTextEdit>
#include <QDebug>

#include "ui_pageswidget.h"
#include "dragabletab.h"
#include "dropmanager.h"

#include "tabsscrollarea.h"

// TODO:
// ui->tabsScrollArea，可以扩展出来，然后去虚继承dropEvent和dragEvent相关，使得具备拖拽能力和接受能力
PagesWidget::PagesWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::PagesWidget)
{
    ui->setupUi(this);
    mTabsScrollArea = new TabsScrollArea(this);
    mTabsScrollArea->SetParentPagesWidget(this);
    ui->verticalLayout->insertWidget(0, mTabsScrollArea);
    this->setFocusPolicy(Qt::StrongFocus);
    ConnectAll();
}

PagesWidget::~PagesWidget() {
    delete ui;
}

void PagesWidget::ConnectAll()
{
    connect(mTabsScrollArea, &TabsScrollArea::pageAdded, this, [this](QWidget* content) {
        ui->contentLayout->addWidget(content);
    });
    connect(mTabsScrollArea, &TabsScrollArea::pageRemoved, this, [this](QWidget* content) {
        ui->contentLayout->removeWidget(content);
    });
    // 待定
    connect(mTabsScrollArea, &TabsScrollArea::closed, this, [this]() {
        this->close();
    });
}

std::pair<int, int> PagesWidget::Id() const
{
    return mTabsScrollArea->Id();
}

void PagesWidget::SetId(int dId, int pId)
{
    mTabsScrollArea->SetId(dId, pId);
}

DroppableWidget* PagesWidget::ParentDroppableWidget()
{
    return mDroppableWidget;
}

void PagesWidget::SetParentDroppableWidget(DroppableWidget* droppableWidget)
{
    mDroppableWidget = droppableWidget;
}

std::pair<DragableTab*, QWidget*> PagesWidget::CurrentPage() const
{
    return mTabsScrollArea->CurrentPage();
}

TabsScrollArea* PagesWidget::GetTabsScrollArea() const {
    return mTabsScrollArea;
}

void PagesWidget::CloseTabFromButton()
{
}

void PagesWidget::AddPage(DragableTab* tab, QWidget* content)
{
    if (tab == nullptr || content == nullptr) {
        return;
    }
    mTabsScrollArea->AddPage(tab, content);
}

void PagesWidget::AddPage(QString titleLabel, QWidget* content, DragableTab::TabType tabType)
{
    if (content == nullptr) {
        return;
    }
    mTabsScrollArea->AddPage(titleLabel, content, tabType);
}

// discard
void PagesWidget::SwapPage(const int from, const int to)
{
    mTabsScrollArea->SwapPage(from, to);
    // auto contents = mTabsScrollArea->SwapPage(from, to);
    // auto fromContent = contents.first;
    // auto toContent = contents.second;
    // if (fromContent == nullptr || toContent == nullptr) {
    //     return;
    // }
    // 这一部分需要pagesWidgets的layout自行去处理
    // ui->contentLayout->removeWidget(fromContent);
    // ui->contentLayout->removeWidget(toContent);
    // // from > to / from < to
    // if (from > to) {
    //     ui->contentLayout->insertWidget(to, fromContent);
    //     ui->contentLayout->insertWidget(from, toContent);
    // } else {
    //     ui->contentLayout->insertWidget(from, toContent);
    //     ui->contentLayout->insertWidget(to, fromContent);
    // }
}

// discard
std::pair<DragableTab*, QWidget*> PagesWidget::ReplacePage(DragableTab* fromTab, DragableTab* toTab, QWidget* toContent)
{
    return mTabsScrollArea->ReplacePage(fromTab, toTab, toContent);;
}

// discard
void PagesWidget::InsertPage(const int index, DragableTab* tab, QWidget* content)
{
    if (tab == nullptr || content == nullptr) {
        return;
    }
    mTabsScrollArea->InsertPage(index, tab, content);
    // ui->contentLayout->addWidget(content);
}

std::pair<DragableTab*, QWidget*> PagesWidget::RemovePage(const int index)
{
    auto page = mTabsScrollArea->RemovePage(index);
    if (page.first == nullptr || page.second == nullptr) {
        return std::make_pair(nullptr, nullptr);
    }

    ui->contentLayout->removeWidget(page.second);
    return page;
}

std::pair<DragableTab*, QWidget*> PagesWidget::RemovePage(const DragableTab *tab)
{
    auto page = mTabsScrollArea->RemovePage(tab);
    if (page.first == nullptr || page.second == nullptr) {
        return std::make_pair(nullptr, nullptr);
    }

    ui->contentLayout->removeWidget(page.second);
    return page;
}

void PagesWidget::closeEvent(QCloseEvent *event)
{
    // 在这里添加关闭窗口时的处理逻辑
    qDebug() << "PagesWidget::closeEvent, 窗口被关闭";
    emit closed();
    event->accept();
}
